import sys
import database
import remindui
from PySide6.QtUiTools import QUiLoader
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtGui import QStandardItemModel, QStandardItem, QIcon

Smarttask_ui_path = './ui/Smarttask.ui'
Error_ui_path = './ui/Error.ui'
Icon_path = './ui/icon.png'

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        database.init_DB()
        database.backup_DB()
        super().__init__(parent)
        loader = QUiLoader()
        self.Smarttask_ui = loader.load(f"{Smarttask_ui_path}", self)
        self.Error_ui = loader.load(f"{Error_ui_path}", self)
        self.setWindowIcon(QIcon(f"{Icon_path}"))
        self.text_show()
        self.show()
        self.Smarttask_ui.add.clicked.connect(self.add_task)
        self.Smarttask_ui.delete_2.clicked.connect(self.delete_task)
        self.Smarttask_ui.quit.clicked.connect(self.quit)
        self.Smarttask_ui.complete.clicked.connect(self.set_task_completed)
        self.Smarttask_ui.change.clicked.connect(self.change_task)
        # 假设UI文件中有一个reminder按钮
        if hasattr(self.Smarttask_ui, 'reminder'):
            self.Smarttask_ui.reminder.clicked.connect(self.show_reminder)

    def text_show(self):
        self.tasks = database.read_DB()
        self.model = QStandardItemModel()
        self.model.setColumnCount(len(self.tasks[0]))
        self.model.setHorizontalHeaderLabels(['ID', '任务标题', '任务类别', '任务优先级', '任务截止日期', '任务状态'])
        for task in self.tasks:
            row_items = []
            for field in task:
                item = QStandardItem(str(field))
                row_items.append(item)
            self.model.appendRow(row_items)
        self.Smarttask_ui.tableView.setModel(self.model)
        column_widths = [30, 200, 60, 80, 100, 80]
        for i, width in enumerate(column_widths):
            self.Smarttask_ui.tableView.setColumnWidth(i, width)

    def error_ui(self):
        self.Error_ui.show()
        self.Error_ui.pushButton.clicked.connect(self.Error_ui.close)

    def judgment(self, title, category, priority, due_date):
        if title == '' or category == '' or priority == '' or due_date == '':
            self.error_ui()
            return False
        elif priority != '1' and priority != '2' and priority != '3':
            self.error_ui()
            return False
        else:
            return True
    
    def judgmentid(self, id):
        if id == '':
            self.error_ui()
            return False
        elif int(id) not in range(1, len(self.tasks)+1):
            self.error_ui()
            return False
        else:
            return True
        
    def add_task(self):
        title = self.Smarttask_ui.title.text()
        category = self.Smarttask_ui.category.text()
        priority = self.Smarttask_ui.priority.text()
        due_date = self.Smarttask_ui.due_date.text()
        if self.judgment(title, category, priority, due_date):
            database.add_task(title, category, priority, due_date)
            self.Smarttask_ui.title.clear()
            self.Smarttask_ui.category.clear()
            self.Smarttask_ui.priority.clear()
            self.Smarttask_ui.due_date.clear()
            self.text_show()

    def delete_task(self):
        delete_id = self.Smarttask_ui.deleteid.text()
        if self.judgmentid(delete_id):
            database.delete_task(delete_id)
            self.Smarttask_ui.deleteid.clear()
            self.text_show()
        else:
            self.Smarttask_ui.deleteid.clear()
    
    def change_task(self):
        changeid = self.Smarttask_ui.changeid.text()
        title = self.Smarttask_ui.title_2.text()
        category = self.Smarttask_ui.category_2.text()
        priority = self.Smarttask_ui.priority_2.text()
        due_date = self.Smarttask_ui.due_date_2.text()
        if self.judgmentid(changeid) and self.judgment(title, category, priority, due_date):
            database.update_task(changeid, title, category, priority, due_date)
            self.Smarttask_ui.changeid.clear()
            self.Smarttask_ui.title_2.clear()
            self.Smarttask_ui.category_2.clear()
            self.Smarttask_ui.priority_2.clear()
            self.Smarttask_ui.due_date_2.clear()
            self.text_show()
        else:
            self.Smarttask_ui.changeid.clear()

    def set_task_completed(self):
        completeid = self.Smarttask_ui.completeid.text()
        if completeid != '':
            if int(completeid) in range(1, len(self.tasks)+1):
                database.set_task_completed(completeid)
                self.Smarttask_ui.completeid.clear()
                self.text_show()
            else:
                self.Smarttask_ui.completeid.clear()
                self.error_ui()
        else:
            self.error_ui()

    def quit(self):
        database.backup_DB()
        QApplication.instance().quit()

    def show_reminder(self):
        self.remind_window = remindui.RemindWindow(self)
        self.remind_window.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())
